#include <cstdio>
#include <iostream>
#include <csignal>

//const PAN_CHAR_T PANTHEIOS_FE_PROCESS_IDENTITY[] = "efs";


#include "common/log.hpp"
#include "common/Utils.hpp"
#include "tcp/TCPMessengerPlug.hpp"
#include "tcp/TCPListenerPlug.hpp"
#include "file-client/FileClientPlug.hpp"
#include "file-server/FileServerPlug.hpp"
#include "folder-manager/FolderManager2.hpp"

using namespace efs;


int main(int argc, char *argv[])
{

	const int items_count = 3;

	Settings settings;
	FolderManager *folder_manager[items_count];
	FileClientPlug file_client_plug[items_count];
	//FileServerPlug file_server_plug[items_count];
	TCPListenerPlug tcp_listener_plug[items_count];
	TCPMessengerPlug tcp_messenger_plug[items_count];

	for (int i = 0; i < items_count; ++i)
	{
		folder_manager[i] = new FolderManager("test_folder_" + intToStr(i+1), 1, settings);
		folder_manager[i]->setExternalRes(&file_client_plug[i], &tcp_messenger_plug[i]);
		tcp_listener_plug[i].setSubscriber(folder_manager[i]);
	}

	for (int i = 0; i < items_count; ++i)
	{
		for (int j = 0; j < items_count; ++j)
		{
			if (i != j)
			{
				tcp_messenger_plug[i].addTCPListener(&tcp_listener_plug[j]);
			}
		}
	}


	for (int i = 0; i < items_count; ++i)
	{
		folder_manager[i]->start();
	}

	char q = ' ';
	while (q != 'q')
	{
		std::cin >> q;
	}

	for (int i = 0; i < items_count; ++i)
	{
		folder_manager[i]->stop();
	}

	for (int i = 0; i < items_count; ++i)
	{
		delete folder_manager[i];
	}



	/*
	Settings settings;
	settings.loadCommandLine(argc, argv);

	pantheios::log_INFORMATIONAL("Start");
	FolderSerializer fs;
	rapidxml::xml_document<> xml;
	fs.clearXML(xml);
	fs.makeFolderXML(xml, "test_folder_1/");

	std::string xml_string;
	rapidxml::print(std::back_inserter(xml_string), xml);

	std::cout << xml_string << std::endl;

	pantheios::log_INFORMATIONAL("Exit");

	return 0;
	*/
}

